// var mongoose = require("mongoose");
// var Schame = mongoose.Schema;

// var OrderSchame = new Schame({
//   order_number: String,
//   number:String,
//   price:String,
//   date_time:Date,
//   user_id:String,
//   remarks:String,
//   food_detail:[{
    
//   }]

// })

// 获取订单
exports.getOrders = function(req,res){
  req.getConnection(function(err,conn){
		if(err){
			console.log("Failed to connect");
			res.status(500).json(err);
		};

		var query = conn.query("SELECT * FROM (order_detail_list LEFT JOIN menu_list ON order_detail_list.goods_id=menu_list.id) LEFT JOIN order_list ON order_detail_list.order_id=order_list.order_id ",function(err,rows){
			if(err){
				console.log(err);
			}else{
				//console.log(typeof(rows));
				
				res.status(200).json(rows);
			}
		})
	})
}


// 生成订单
exports.generateOrders = function(req,res){

	// var order_data = req.body;
	console.log(req.body);

    req.getConnection(function(err,conn){
		if(err){
			console.log("Failed to connect");
			res.status(500).json(err);
		};
    
		var data = {
         order_id : req.body.order_number,
				 table_number :  req.body.number,
				 total_price : req.body.money,
				 order_time : req.body.time,
				 remarks : req.body.remarks
		}
		var query = conn.query("INSERT INTO order_list SET ?",data,function(err,rows){
			if(err){
				res.status(500).json(err);
			}else{
				console.log("1 order generated!")
			}
		});

	//	var len = req.body.food_detail.length();
		req.body.food_detail.forEach(function(element) {
			var data = {
				order_id : req.body.order_number,
				goods_id : element.id,
				goods_number : element.number,
				price : element.price ,
				price_medium : element.price_medium,
				price_large : element.price_large
			}
			var query_detail = conn.query("INSERT INTO order_detail_list SET ?",data,function(err,rows){
				if(err){
					console.log("failed to insert");
					console.log(err);
				//	res.status(500).json(err);
				}else{
					console.log("1 record inserted");
				}
			})
		}, this);
   		res.redirect('/#/app/orderCash');
	})

};







